package math.base;

/**
 * @author Saga
 *
 */
public class Dir2d {
    public Dir2d() {
        this.coord = new Coord2d(1.0, 0.0);
    }

    public Dir2d(final double _x, final double _y) {
        final double mag = Math.sqrt(_x * _x + _y * _y);
        final double scalar = 1.0 / mag;
        this.coord = new Coord2d(_x * scalar, _y * scalar);
    }

    public Dir2d(final Coord2d _coord) {
        this.coord = _coord.normalized();
    }

    public Dir2d(final Vector2d _vec) {
        this.coord = _vec.getCoord().normalized();
    }

    //coord
    public final void setNormalizedCoord(final Coord2d _coord) {
        this.coord = _coord;
    }

    public final void setNormalizedCoord(final double _x, final double _y) {
        this.coord.init(_x, _y);
    }

    public final void setCoord(final Coord2d _coord) {
        this.coord = _coord.normalized();
    }

    public final Coord2d getCoord() {
        return this.coord;
    }

    public final void setCoord(final double _x, final double _y) {
        this.coord = new Coord2d(_x, _y);
        this.coord.normalize();
    }

    public final double getX() {
        return this.coord.getX();
    }

    public final double getY() {
        return this.coord.getY();
    }

    //reverse
    public final void reverse() {
        this.coord.reverse();
    }

    public final Dir2d reversed() {
        return new Dir2d(this.coord.reversed());
    }

    //multiply
    public final Vector2d multiplied(final double _scalar) {
        return new Vector2d(this.coord.multiplied(_scalar));
    }

    //product
    public final double dot(final Dir2d _dir) {
        return this.coord.dot(_dir.getCoord());
    }

    public final double crossed(final Dir2d _dir) {
        return this.getX() * _dir.getY() - this.getY() * _dir.getX();
    }

    @Override
    public final Dir2d clone() {
        final Dir2d output = new Dir2d();
        output.setNormalizedCoord(this.coord.clone());
        return output;
    }

    private Coord2d coord;
}
